//
//  LGMineViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/10.
//

#import "LGMineViewController.h"
//个人资料
#import "LGPersionalDataController.h"
//服务统计
#import "LGServiceStatisticsController.h"
//设置
#import "LGSettingViewController.h"
//帮助与客服
#import "LGHelpServiceController.h"
#import "LGCustomerServiceController.h"
//排班管理
#import "LGReceptionSettingController.h"
//团队管理
#import "LGTeamManageController.h"
//我的订单
#import "LGOrderViewController.h"
#import "LGSportOrderViewController.h"
#import "LGServiceOrderViewController.h"
//运动评估
#import "LGSportsEvaRecordController.h"
//康复方案
#import "LGRecoveryProgramController.h"
//药物库
#import "LGSearchMedicineController.h"
//慢病服务
#import "LGServiceDetailController.h"
//快捷回复
#import "LGQuickReplyController.h"
//上传报告文件
#import "LGUploadFilesController.h"

#import "LGMineHomeHeaderView.h"
#import "LGMineHomeFooterView.h"
#import "LGMineHomeItemCell.h"
#import "LGCollectionViewLeftFlowLayout.h"
@interface LGMineViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,LGMineHomeFooterDelegate>
/**   */
@property (nonatomic , strong)UIImageView *bgImageView;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)NSDictionary *doctorDic;
/**   */
@property (nonatomic , strong)NSArray *itemArray;

@end

@implementation LGMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.gradientLayer.hidden = YES;
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#F6F8FC"];
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.collectionView];
    [self requestData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self viewWillAppearAction];
}

-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.index" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.doctorDic = returnData[@"doctor"];
            self.itemArray = returnData[@"statistics"];
            [self.collectionView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//查看个人信息
-(void)checkPersonalInfo:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.bgImageView];
    if(CGRectContainsPoint(CGRectMake(viewPix(16), statusBarHeight+24, Screen_W-viewPix(32), viewPix(64)), point)){
        LGPersionalDataController *controller = [[LGPersionalDataController alloc]init];
        controller.userId = kUserId;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//服务统计
-(void)serviceItemTouched:(NSString *)type{
    if([type isEqualToString:@"patient"]){
        //患者--患者管理
        self.tabBarController.selectedIndex = 2;
        
    }else if ([type isEqualToString:@"vip"]){
        //服务包
        LGServiceOrderViewController *controller = [[LGServiceOrderViewController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"clinic"]){
        //慢病复查-线下复查订单
        LGOrderViewController *controller = [[LGOrderViewController alloc]init];
        controller.type = @"1";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"video"]){
        //视频咨询
        LGOrderViewController *controller = [[LGOrderViewController alloc]init];
        controller.type = @"2";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"expert"]){
        //专家咨询
        LGOrderViewController *controller = [[LGOrderViewController alloc]init];
        controller.type = @"3";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"phone"]){
        //电话咨询
        LGOrderViewController *controller = [[LGOrderViewController alloc]init];
        controller.type = @"6";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"team"]){
        //团队咨询
        LGOrderViewController *controller = [[LGOrderViewController alloc]init];
        controller.type = @"4";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([type isEqualToString:@"sport"]){
        //运动评估订单
        LGSportOrderViewController *controller = [[LGSportOrderViewController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//常用工具
-(void)commonToolItemTouched:(NSString *)title{
    if ([title isEqualToString:@"我的团队"]){
        LGTeamManageController *controller = [[LGTeamManageController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([title isEqualToString:@"快捷回复"]){
        LGQuickReplyController *controller = [[LGQuickReplyController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([title isEqualToString:@"药物库"]){
        LGSearchMedicineController *controller = [[LGSearchMedicineController alloc]init];
        controller.canSelect = NO;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([title isEqualToString:@"操作指南"]){
        LGHelpServiceController *controller = [[LGHelpServiceController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if ([title isEqualToString:@"上传报告"]){
        LGUploadFilesController *controller = [[LGUploadFilesController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
    }else if ([title isEqualToString:@"设置"]){
        LGSettingViewController *controller = [[LGSettingViewController alloc]init];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGMineHomeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.showTip = (self.itemArray.count == 2 || self.itemArray.count == 4)?YES:NO;
    cell.dataDic = self.itemArray[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.itemArray[indexPath.item];
    [self serviceItemTouched:LGNSString(tempDic[@"type"])];
}
/*
 
 */

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (self.itemArray.count == 2 || self.itemArray.count == 4)?(Screen_W-viewPix(32))/2.0:(Screen_W-viewPix(40))/3.0;
    return CGSizeMake(width, viewPix(106));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(Screen_W, topBarHeight+viewPix(93));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(Screen_W, viewPix(230));
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if(kind == UICollectionElementKindSectionHeader){
        LGMineHomeHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        headView.dataDic = self.doctorDic;
        [headView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkPersonalInfo:)]];
        return headView;
        
    }else if(kind == UICollectionElementKindSectionFooter){
        LGMineHomeFooterView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footView.delegate = self;
        return footView;
        
    }else{
        UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"normalFooter" forIndexPath:indexPath];
        footView.backgroundColor = [UIColor clearColor];
        return footView;
    }
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        LGCollectionViewLeftFlowLayout *layout = [[LGCollectionViewLeftFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumInteritemSpacing = viewPix(8);
        layout.minimumLineSpacing = viewPix(8);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-tabBarHeight) collectionViewLayout:layout];
        [_collectionView registerClass:[LGMineHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [_collectionView registerClass:[LGMineHomeFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"normalFooter"];
        [_collectionView registerClass:[LGMineHomeItemCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.contentInset = UIEdgeInsetsMake(0, viewPix(12), 0, viewPix(12));
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(UIImageView *)bgImageView{
    if(!_bgImageView){
        _bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的-背景"]];
        _bgImageView.frame = CGRectMake(0, 0, Screen_W, LGImageHeight(Screen_W, 1125/3600.0));
        _bgImageView.userInteractionEnabled = YES;
        _bgImageView.clipsToBounds = YES;
    }
    return _bgImageView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
